<?php

require_once dirname(__FILE__) . '/../Board/SpaceMaker.php';

class Monopoly_Board
{
    private $_spaces = null;

    public function __construct($space_maker)
    {
        $spaces = $space_maker->make('Go', $this);
        $spaces->next($space_maker->make('Mediterranean Avenue', $this))
               ->next($space_maker->make('Community Chest', $this))
               ->next($space_maker->make('Baltic Avenue', $this))
               ->next($space_maker->make('Income Tax', $this))
               ->next($space_maker->make('Reading Railroad', $this))
               ->next($space_maker->make('Oriental Avenue', $this))
               ->next($space_maker->make('Chance', $this))
               ->next($space_maker->make('Vermont Avenue', $this))
               ->next($space_maker->make('Connecticut Avenue', $this))
               ->next($space_maker->make('Just Visiting', $this))
               ->next($space_maker->make('St. Charles Place', $this))
               ->next($space_maker->make('Electric Company', $this))
               ->next($space_maker->make('States Avenue', $this))
               ->next($space_maker->make('Virginia Avenue', $this))
               ->next($space_maker->make('Pennsylvania Railroad', $this))
               ->next($space_maker->make('St. James Place', $this))
               ->next($space_maker->make('Community Chest', $this))
               ->next($space_maker->make('Tennessee Avenue', $this))
               ->next($space_maker->make('New York Avenue', $this))
               ->next($space_maker->make('Free Parking', $this))
               ->next($space_maker->make('Kentucky Avenue', $this))
               ->next($space_maker->make('Chance', $this))
               ->next($space_maker->make('Indiana Avenue', $this))
               ->next($space_maker->make('Illinois Avenue', $this))
               ->next($space_maker->make('B&O Railroad', $this))
               ->next($space_maker->make('Atlantic Avenue', $this))
               ->next($space_maker->make('Ventnor Avenue', $this))
               ->next($space_maker->make('Water Works', $this))
               ->next($space_maker->make('Marvin Gardens', $this))
               ->next($space_maker->make('Go To Jail', $this))
               ->next($space_maker->make('Pacific Avenue', $this))
               ->next($space_maker->make('North Carolina Avenue', $this))
               ->next($space_maker->make('Community Chest', $this))
               ->next($space_maker->make('Pennsylvania Avenue', $this))
               ->next($space_maker->make('Short Line', $this))
               ->next($space_maker->make('Chance', $this))
               ->next($space_maker->make('Park Place', $this))
               ->next($space_maker->make('Luxury Tax', $this))
               ->next($space_maker->make('Boardwalk', $this))
               ->next($spaces);

        $this->setSpaces($spaces);
    }

    public function setSpaces($spaces)
    {
        $this->_spaces = $spaces;
    }

    public function getSpaces()
    {
        return $this->_spaces;
    }

    public function getSpace($name)
    {
        $spaces = $this->getSpaces();

        $start_name = $spaces->getName();

        while (true) {
            if ($name == $spaces->getName()) {
                return $spaces;
            }

            $spaces = $spaces->next();

            if ($spaces->getName() == $start_name) {
                return false;
            }
        } 
    }
}
